function expSaverFunc( stimFileNames, electroExpName, electroExp,...
                       electroExpType )
%expSaverFunc saves an experiment from the ExpMaker gui. It implements
%uiputfile to save to a user specified location
% INPUTS:           state: state of the ExpMaker Gui
%                   Exp:   Experiment structure created through the
%                          ExpMaker gui
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%copyright (c) 2012  Matthew Caudill
%
%this program is free software: you can redistribute it and/or modify
%it under the terms of the gnu general public license as published by
%the free software foundation, either version 3 of the license, or
%at your option) any later version.
%
%this program is distributed in the hope that it will be useful,
%but without any warranty; without even the implied warranty of
%merchantability or fitness for a particular purpose.  see the
%gnu general public license for more details.
%
%you should have received a copy of the gnu general public license
%along with this program.  if not, see <http://www.gnu.org/licenses/>.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% LOAD DIRECTORY INFORMATION %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
electroExpDirInformation
% depending on the type of electroExp to be saved, we have different saving
% locations as specified in electroExpDirInformation. We use a switch case
% to determine the target location for our save.
switch electroExpType
    case 'raw'
        expTargetLoc = eDirInfo.electroExpRawFileLoc;
    case 'analyzed'
        expTargetLoc = eDirInfo.electroExpAnalyzedFileLoc;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% CONSTRUCT DEFAULT NAME FOR EXP %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Depending on whether the electroExpType is raw, or analyzed, we need to
% construct a default save file name. If the imExp is type raw (meaning
% created by the ExpMaker), we must use the stimulus File Names to create
% an imExp Name. If the Exp being created is type analyzed then
% we create the imExp name from the pre-existing Exp name generated by
% the ExpMaker gui

switch electroExpType
    case 'raw'
        
        % split the stimFileName on the underscores returning an allStrings
        % cell array
        allStrings = regexp(stimFileNames{1},'_', 'split');
        % obtain the date
        date = allStrings{2};
        % obtain the location and expType identifier
        locExpType = allStrings{3};

        defaultName =...
            [expTargetLoc,date,'_',locExpType,'_','electroExp','.mat'];
    
    case 'analyzed'
        % either the ExpMaker or an analysis 
        % script is calling. So we use the
        % pre-existing Exp name to create a new
        % ExpName
        
        % split the ExpName on the underscores
        allStrings = regexp(ExpName,'_', 'split');
        % obtain the date
        date = allStrings{1};
        % obtain the location and expType identifier
        locExpType = allStrings{2};
        
        defaultName = [expTargetLoc, date,'_', locExpType, '_',...
               'electroExp_Analyzed', '.mat'];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% CONSTRUCT PATH AND FILE NAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[fileName,pathName] = uiputfile(expTargetLoc,'Save As',defaultName);
file = fullfile(pathName,fileName);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SAVE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ischar(fileName) && ischar(pathName) 
    save(file, '-struct', 'electroExp');
else
    warndlg('WARNING: EXPERIMENT NOT SAVED')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

